package com.atguigu.gmall.payment.demo;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

public class AlipayDemo {

    //统一收单下单并支付页面接口
    public static void main(String[] args) throws Exception{
        //公共参数
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",//沙箱地址
                "2021000118661410",//沙箱的账户
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCPBaO9XYkjJxd0TgJ8khhJJOkzmjm5OdHid3SxPtspcWqcc/Is0D3X9KCNkDkCQ/t8YKniQaPdd5w7c5qbKjbyW+oFZ0SU0Ta45v/Z8OgtwQuIFbxOPBEvDxsfzj5E0LmgDjIwBxsZE6ODmZvf+Nz+87EzB2hQL1p45vrvXnWJlzi7SKyuv1EQ24BCSo2WkxR7OKT7z/tSXy6mVhFcB8k/JBg9gmqsGlpL97BNFUE2QTobpFrZSRp3QAUz8CLX/tueg2lRPR2TdP5cazfpNdCZCLYq+9uIV3zPrlgTUHsCRwrN4MMTFNxvJND/HDuSHGPepLQashRxMLHrKaC5C/y3AgMBAAECggEASTAon2H7KIr7nJ/C8/5LATsrPyXf/iJRfmOEyqMQwf9xAAjxV+9khlt/wZGZKsihK//HT6opcc6gm2fIKTkMDy/r7MHwvXrmVu6W5O870RYlkYkV6K8iWAOR7M+827g4FWjOU/d+7XE4TsLPmmieprUL68IRpl7lHFOO++lJmpOJT85XCc3rXZf6K6fELjZwTEhpU5pT15QnV1TWx3PJJxQWBCS8t0di/56QrMeGBDiE32v+lbNV9Mbes9Qa2NZUzFYntKgRS3cE1vtbxB90N6B34/7Eea/kE6IKSMA82SyRbf70cro6kbISLATZSqiiZm5syVFQaV0ubvUvreiAmQKBgQDpgn/2MkZGzfbU/yZP+xowkZEUltJf2gPg1J1anczpqZ67SrzUfa0e465TdxJbTekTLY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UvAdp/gHYHQNnto4U1okvKe7/y+SnpBkos8KmHKHBZJvT9LnV/TNYMrUuX1KTrLBQXdWBct/bd15gycInkM+XHEVgq5NBj70QYAsgaQuePQBHbRHFxBbod/uPs9Q5KcMxadzHM92fgwmThGZugYzQMRH4TqtXxKI45nCaDQIDAQAB",//支付宝公司的公钥
                "RSA2");//加密方式
        //请求参数
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl("");//异步回调
        request.setReturnUrl("");//同步回调
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "20210817010101004");
        bizContent.put("total_amount", 0.01);
        bizContent.put("subject", "测试商品");
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        //bizContent.put("time_expire", "2022-08-01 22:00:00");

        //// 商品明细信息，按需传入
        //JSONArray goodsDetail = new JSONArray();
        //JSONObject goods1 = new JSONObject();
        //goods1.put("goods_id", "goodsNo1");
        //goods1.put("goods_name", "子商品1");
        //goods1.put("quantity", 1);
        //goods1.put("price", 0.01);
        //goodsDetail.add(goods1);
        //bizContent.put("goods_detail", goodsDetail);

        //// 扩展信息，按需传入
        //JSONObject extendParams = new JSONObject();
        //extendParams.put("sys_service_provider_id", "2088511833207846");
        //bizContent.put("extend_params", extendParams);

        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        if(response.isSuccess()){
            System.out.println(response.getBody());
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
